#include <time.h>
#include "utils.c"
#include "bruteforce.c"
#include "horspool.c"
#include "bndm.c"
#include "bom.c"


int main(int argc, char *argv[])
{
    int alphabet_size;
    char filename[256];
    char *text;
    int n;

    int a;
    int i;
    char pattern[256];
    struct timeval start, end;
    double seconds;
    long int count;

    // Read file
    alphabet_size = atoi(argv[1]);
    sprintf(filename, "%s", argv[2]);
    n = readfile(filename, &text, alphabet_size);

    // Search
    for (a = 0; a < 4; a++) {
        for (i = 3; i < argc; i++) {
            sprintf(pattern, "%s", argv[i]);
            switch(a) {
                case 0:
                    gettimeofday(&start, NULL);
                    count = BF(pattern, strlen(pattern), text, n);
                    gettimeofday(&end, NULL);
                    break;
                case 1:
                    gettimeofday(&start, NULL);
                    count = HORSPOOL(pattern, strlen(pattern), text, n);
                    gettimeofday(&end, NULL);
                    break;
                case 2:
                    gettimeofday(&start, NULL);
                    count = BNDM(pattern, strlen(pattern), text, n);
                    gettimeofday(&end, NULL);
                    break;
                case 3:
                    gettimeofday(&start, NULL);
                    count = BOM(pattern, strlen(pattern), text, n);
                    gettimeofday(&end, NULL);
                    break;

            }
            seconds = ((end.tv_sec * 1000000 + end.tv_usec) -
                       (start.tv_sec * 1000000 + start.tv_usec));
            seconds /= (double) 1000000;
            printf("%s,%d,%f\n", pattern, count, seconds);
            fflush(stdout);
        }
    }
    free(text);
    return 0;
}
